<?php

declare(strict_types=1);

namespace app\command;

use DirectoryIterator;
use SplFileInfo;

// 引入框架内置类
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use think\File;

class FileConsole extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('fileconsole')
            ->addArgument('name', Argument::OPTIONAL, "console name")
            ->addOption('dir', null, Option::VALUE_REQUIRED, 'directory name')
            ->addOption('savedir', null, Option::VALUE_REQUIRED, 'savedir name')
            ->setDescription('the fileconsole command');
    }

    protected function execute(Input $input, Output $output)
    {
        $name = trim($input->getArgument('name'));
        $name = $name ?: 'thinkphp';

        if ($input->hasOption('dir')) {
            $dir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('dir'));
        } else {
            $dir = str_replace("\\", '/', Config::get('filesystem.disks.public.root')) . '/';
        }
        if ($input->hasOption('savedir')) {
            $savedir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('savedir'));

            // 指令输出
            $output->writeln('fileconsole :' . $name . "--dir: " . $dir . '--savedir' . $savedir);

            $this->$name($dir, $savedir);
        } else {
            // 指令输出
            $output->writeln('fileconsole :' . $name . "--dir: " . $dir);
    
            $this->$name($dir);
        }

        $output->writeln("\n..ok");
    }

    public function thinkphp()
    {
        echo "hello thinkphp";
    }

    /**
     * 建立文件管理目录索引(递归遍历目录)
     */
    public function scan($dir)
    {
        echo "test" . $dir;
    }

    /**
     * 建立文件管理目录索引(递归遍历目录)
     */
    public function markwater($dir, $savepath = 'test/cpw', $watermark = "test/watermark2.png")
    {
        $source = str_replace("\\", '/', app()->getRootPath() . $watermark);

        $savepath = str_replace("\\", '/', app()->getRootPath() . $savepath);

        if (is_dir($dir)) {
            $dirs = new DirectoryIterator($dir);

            foreach ($dirs as $fileInfo) {
                if ($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
                    continue;
                }

                $filename = $dirs->getPath() . '/' . $fileInfo->getFilename();
                $savename = str_replace("\\", '/', $savepath . '/' . $fileInfo->getFilename());

                echo $filename . "\n";
                echo $savename . "\n";

                $image = \think\Image::open($filename);

                $image->water($source, \think\Image::WATER_NORTHWEST)->save($savename);

                $image = null;
            }
        }
    }

    /**
     * 重置图片文件大小
     */
    public function resize($dir, $savepath = 'test/resize', $xmax = 1000, $ymax = 1000)
    {
        $savepath = str_replace("\\", '/', app()->getRootPath() . $savepath);
        
        if (!file_exists($savepath)) {
            mkdir($savepath, 0755);
        }

        if (is_dir($dir)) {
            $dirs = new DirectoryIterator($dir);

            foreach ($dirs as $fileInfo) {
                if ($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
                    continue;
                }

                $filename = $dirs->getPath() . '/' . $fileInfo->getFilename();
                $savename = str_replace("\\", '/', $savepath . '/' . $fileInfo->getFilename());

                echo $filename . "\n";
                echo $savename . "\n";

                $image = \think\Image::open($filename);

                $image->thumb($xmax, $ymax)->save($savename);

                $image = null;
            }
        }
    }
}
